<?php
namespace app\api\controller;

use app\admin\model\software\Upgrade;
use app\common\controller\Api;
use think\Env;
use think\Request;

// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    // 响应类型
    header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
    // 带 cookie 的跨域访问
    header('Access-Control-Allow-Credentials: true');
    // 响应头设置
    header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token,token');

    exit("ok");
}

/**
 * OTA管理
 * @apiDefine auth_token 全局配置token鉴权请求头
 * @apiHeader {String} [token=355ac501-2d3b-4bd9-9b53-0cac6e8147ce] 用户登录授权token（需要用户登录授权的接口必须）
 */
class Device  extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $host = '';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->host = Env::get('app.host');
    }

    /**
     * @api {post} /api/device/handleDevice 1.2-设备处理
     * @apiSampleRequest /api/device/handleDevice
     *
     * @apiGroup 1-设备本版管理
     * @apiName handleDevice
     * @apiDescription 设备本版管理
     * @apiUse auth_token
     * @apiVersion 1.0.0
     *
     * @apiBody {String}  customer     客户
     * @apiBody {String}  mode         型号
     * @apiBody {String}  version      数字版本号
     * @apiBody {String}  softVersion  软件完整版本号
     * @apiBody {String}  iccid        iccid
     * @apiBody {String}  mac          mac(网卡地址)
     * @apiBody {String}  signal       信号
     * @apiBody {String}  slot         当前网络卡槽
     *
     * @apiSuccess {int{-999-999}} result.code 响应代码<code>{1:成功，1000:设备已被禁用, 1002: 入库信息不存在, <0:失败}</code>
     * @apiSuccessExample Success-Response:
     * {
     *   "code": 1,
     *   "msg": "success",
     *   "time": "1728614647",
     *   "data": "文件地址"
     * }
     */
    public function handleDevice()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $customer = isset($param['customer']) ? $param['customer'] : '';
            $mode = isset($param['mode']) ? $param['mode'] : '';
            $version = isset($param['version']) ? $param['version'] : '';
            $softVersion = isset($param['softVersion']) ? $param['softVersion'] : '';
            $iccid = isset($param['iccid']) ? $param['iccid'] : '';
            $mac = isset($param['mac']) ? $param['mac'] : '';
            $signal = isset($param['signal']) ? $param['signal'] : '';
            $slot = isset($param['slot']) ? $param['slot'] : 0; //热点连接数
            if (empty($customer)) {
                $this->error('');
            }
            if (empty($mode)) {
                $this->error('');
            }
            if (empty($version)) {
                $this->error('');
            }
            // 判断设备版本是否存在，如果存在则访问url,否则返回空
            // 后台两个版本，  我现在接口传的是01版本，但是直接返回03版本，应该要安排版本  返回02的版本的url
            // 数据处理
            $printAry = explode('_', $softVersion);
            $printValue = $printAry[0] . $printAry[1] . $printAry[2];
            $deviceModel = new Upgrade();
            $deviceInfo = $deviceModel->where('customer', $customer)
                ->where('mode', $mode)
                ->where('version', '>', $version)
                ->where('software', $printValue)
                ->find();
            $data = [];
            if ($deviceInfo) {
                $data = $deviceInfo['url'] ? $this->host . $deviceInfo['url'] : '';
            }
            if ($data) {
                $this->success('success', $data);
            }else {
                $this->error('无需版本迭代升级');
            }
        }

        $this->error('请求方法错误');
    }
}